home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 13 / prime2.c < prev    next >
C/C++ Source or Header  |  1988-12-20  |  4KB  |  142 lines

  1. /*    Diana Sysinger
  2.     C Programming
  3.     Extra Credit     */
  4.  
  5. /* a few comments by jack velte */
  6.  
  7. /*    Division by subtraction    with error checking    */
  8.  
  9. /* xxx you sure like extra \n's */
  10.  
  11. #include <stdio.h>
  12. #ifdef notdef
  13. #include "myname.c"    /* xxx what is this?  you shouldn't include */
  14.             /* other C files, but rather link them together */
  15.             /* after all the compiles. */
  16. #endif
  17.  
  18. /* avoid global variables -- here i leave this one. */
  19. int count;
  20.  
  21. /* xxx declare this integer so it return an int */
  22. int getlim(void);    /* gets upper limit from user, must be > 2 */
  23. void prime(const int);    /* finds the prime numbers between 2 and the limit */
  24.             /* calls prtit to do the output    */
  25. void prtit(const int);    /* formats the output, 10 numbers per line  */
  26. void main(void);
  27.  
  28. void
  29. main(void) {
  30.     int limit;        /* xxx these should be local variables */
  31.                 /* NOT global variables.  save global */
  32.                 /* variables when you HAVE to use them. */
  33.     limit = getlim();
  34.     
  35.     prtit(1);        /* uh, my version didn't print 1 as a */
  36.                 /* prime.... */
  37.     prime(limit);
  38.     
  39.     /* xxx redirect the output if you want it to go to the printer */
  40.     /* for example prime > prn  or prime > output.fil */
  41.     printf("\nThere are %d primes from 2 to %d.\n", count, limit);
  42. }
  43.  
  44. int
  45. getlim(void) {
  46. /*    extern int limit;    /* xxx what is this??? this is wrong! */
  47.     int limit;        /* xxx just use a local variable */
  48.  
  49. #ifdef notdef        
  50.     printf("\n\nPlease enter the upper Limit for the prime search.\n");
  51.     printf("The Limit should be 2 or larger.\n");
  52.     scanf("%d", &limit);
  53.  
  54.     if (limit < 2)    {
  55.         /* xxx \n go at the end of most lines. */
  56.         printf("\n\nOops! Limit must be 2 or larger!");
  57.         printf("\nTry again.\n");
  58.         getch();
  59.         getlim();        /* xxx ouch, recursion! */
  60.         }
  61.     else
  62.         fprintf(stdprn,"\nHere come the primes!\n\n");
  63. #else    
  64.     limit = 0;
  65.     while (limit < 2) {
  66.             /* xxx     (v removed `the' so it would fit...) */
  67.         printf("Please enter upper Limit for the prime search.\n");
  68.         printf("The Limit should be 2 or larger.\n");
  69.         scanf("%d", &limit);
  70.         if (limit >= 2)
  71.             /* leave this loop */
  72.             break;
  73.         /* ok, didn't leave, print error message */
  74.         printf("Oops! Limit must be 2 or larger!\n");
  75.         printf("Try again.\n");
  76.         getch();
  77.     }
  78.     
  79.     printf("Here come the primes!\n");
  80.     return (limit);
  81. #endif    
  82. }
  83.  
  84.     /* pass limit as a constant (isn't modified by the function) */
  85.     /* parameter */
  86. void
  87. prime(const int limit) {
  88.     int div = 2;
  89.     int num;
  90.     
  91.     /*
  92.      * i don't really follow this code, but it seems to work.  one
  93.      * comment, though.  multiply and divide are *very* expensive
  94.      * operations relative to most instructions.  if you can factor
  95.      * out the / % and * functions, the algorithm will run much
  96.      * faster.  for this case, it doesn't matter, the program
  97.      * runs quite fast, but in general this is a good thing to
  98.      * watch for.
  99.      */
  100.  
  101.     /* do until limit reached */
  102.     for (num = 2; num <= limit; num++) {    
  103.         for (div = 2; num >= div * div; div++)
  104.                 /* compare to square of divisor    */
  105.                 if (num % div * div == 0)    
  106.                     /* if num/square = 0 then num */
  107.                     /* not a prime number - do loop */
  108.                     break;    
  109.                 
  110.             /* if num/div = zero then check    */
  111.             for (; num % div != 0; div++)
  112.                 /* xxx mark null loop conditions with this */
  113.                 /* comment... */
  114.                 /* void */;    /* for prime condition */
  115.             
  116.             if (div == num)        
  117.                 /* if div = num then num is a */
  118.                 /* prime so print it out */
  119.                 prtit(num);            
  120.     }
  121. }
  122.  
  123.  
  124. void prtit(const int num) {
  125.     /*
  126.      * `count' should be a local not an 'extern'.  extern means it was
  127.      * declared in another module.  static means it starts with 0 and
  128.      * does NOT start over when the module is called the second time.
  129.      * a static variable (in a function) retains it's value between
  130.      * function calls.
  131.      * .... but now i declared count to be global, so there should be
  132.      * no declaration here.
  133.      */
  134.     
  135.     printf("%5d", num);        /* print the numbers 10 to a line. */
  136.     if (++count % 10 == 0)
  137.         printf("\n");
  138. }
  139.  
  140. /*
  141.  * good luck -- your code was good, with only a few rough 'style' edges.
  142.  */